package com.googlecode.allinclusive.fw.core.config;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.MessageSourceResolvable;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.context.support.ResourceBundleMessageSource;

@Deprecated
@SuppressWarnings("rawtypes")
public class ClassMessageUtils {

	private static final Log log = LogFactory.getLog(ClassMessageUtils.class);
	
	// Class MESSAGE
	private static Map<Class, ResourceBundleMessageSource> classMessagesMap = new HashMap<Class, ResourceBundleMessageSource>();
	
	public static ResourceBundleMessageSource getMessageSource(Class clazz) { 
		ResourceBundleMessageSource srcMessage = classMessagesMap.get(clazz);
		if (srcMessage == null) {
			
			String baseName = clazz.getName();
			try {
				ResourceBundle.getBundle(baseName);
			} catch (MissingResourceException mre) {
				return null;
			}
			srcMessage = new ResourceBundleMessageSource();
			srcMessage.setUseCodeAsDefaultMessage(false);
			srcMessage.setBasename(baseName);
			classMessagesMap.put(clazz, srcMessage);
		}
		return srcMessage;
	}

	public static String getMessage(Class clazz, String messageKey) {
		return getMessage(clazz, messageKey, (Object[])null);
	}
	
	public static String getMessage(Class clazz, String messageKey, Object[] args) {
		return getMessage(clazz, messageKey, args, GlobalConfig.DEFAULT_LOCALE);
	}
	
	public static String getMessage(Class clazz, String messageKey, Object[] args, Locale locale) {
		return getMessage(clazz, messageKey, args, (String)null, locale);
	}
	
	public static String getMessage(Class clazz, String messageKey, Object[] args, String defalutMessage, Locale locale) {
		String message = null;
		try {
			ResourceBundleMessageSource msgSrc = getMessageSource(clazz);
			if (msgSrc != null) {
				message = getMessage(msgSrc, messageKey, args, defalutMessage, locale);
			}
		} catch (MissingResourceException mre) {
			message = null;
		} catch (NoSuchMessageException nsme) {
			message = null;
		}
		
		if (message != null) {
			return message;
		} else {
			return MessageUtils.getMessage(messageKey, args, defalutMessage, locale);
		}
	}
	
	public static String getMessage(Class clazz, MessageSourceResolvable resolvable, Locale locale) {
		String message = null;
		try {
			ResourceBundleMessageSource msgSrc = getMessageSource(clazz);
			if (msgSrc != null) {
				message = getMessage(msgSrc, resolvable, locale);
			}
		} catch (MissingResourceException mre) {
			message = null;
		} catch (NoSuchMessageException nsme) {
			message = null;
		}
		
		if (message != null) {
			return message;
		} else {
			return MessageUtils.getMessage(resolvable, locale);
		}
	}
	
	protected static String getMessage(ResourceBundleMessageSource msgSrc, String messageKey, Object[] args, String defalutMessage, Locale locale) {
		String message = msgSrc.getMessage(messageKey, args, defalutMessage, locale);
		if (message != null && !MessageUtils.NATIVE2ASCII) {
			try {
				message = new String(message.getBytes("ISO-8859-1"), GlobalConfig.DEFAULT_ENCODING);
			} catch (UnsupportedEncodingException e) {
				log.warn(e);
			}
		}
		return message;
	}
	
	protected static String getMessage(ResourceBundleMessageSource msgSrc, MessageSourceResolvable resolvable, Locale locale) {
		return msgSrc.getMessage(resolvable, locale);
	}
	
	// --- example ----
	public static void main(String[] args) {
		System.out.println(ClassMessageUtils.getMessage(ClassMessageUtils.class, MESSAGE.ERR_REQUIRED, new Object[] {FIELD.PASSWORD}));
		System.out.println(ClassMessageUtils.getMessage(ClassMessageUtils.class, MESSAGE.ERR_MAXLENGTH, new Object[] {FIELD.PASSWORD, 12}));
		System.out.println(ClassMessageUtils.getMessage(MessageUtils.class, MESSAGE.ERR_MAXLENGTH, new Object[] {FIELD.PASSWORD, 12}, null, Locale.GERMAN));
		
	}
	interface FIELD {
		public static final MessageSourceResolvable USERNAME = new DefaultMessageSourceResolvable("comm.username");
		public static final MessageSourceResolvable PASSWORD = new DefaultMessageSourceResolvable("comm.password");
	}
	interface MESSAGE {
		public static final String ERR_REQUIRED = "error.required";
		public static final String ERR_MAXLENGTH = "error.maxlength";
	}
}
